<template>
	<view>
		<TpCalendar @handleChooseDay="handleChooseDay" :startDate="curChoosedDayObj && curChoosedDayObj.full" :status="goOrBack==='go' ? '去程':'返程'"
		 :addDays="durationDay" :choosedDay="choosedDay"></TpCalendar>
	</view>
</template>

<script>
	import TpCalendar from '@/components/tp-calendar/tp-calendar.vue'
	import calendar from '../../components/tp-calendar/util.js'
	export default {
		components: {
			TpCalendar
		},
		data() {
			return {
				goOrBack: '',
				curChoosedDayObj: {},
				delayDay: 0, // 默认当天
				// durationDay: 60, // 可以选择多少天以内,默认60天
				durationDay: 100800, //小时 可以选择多少天以内,默认60天  小时
				choosedDay: '', // 当前选中的日期
			};
		},
		onLoad(opt) {
			console.log(opt,'传入的时间')
			this.goOrBack = opt && opt.sts
			this.delayDay = opt && opt.delayDay
			this.durationDay = opt && opt.durationDay
			this.choosedDay = opt && opt.choosedDay
			this.init()
		},
		methods: {
			handleChooseDay(item) {
				 console.log(item); // 选择的时间  2020-10-11 
				if (this.goOrBack === 'go') {
					let selectDate = calendar.transdate(item.full) // 选择的时间  //  2020-10-11   ===> 时间戳
					let back_aa = this.$store.getters.getHomeChoosedDateObj.back_full // 返程的时间  2020-10-12
					if (back_aa) {
						let backDate = calendar.transdate(this.$store.getters.getHomeChoosedDateObj.back_full)
						if (selectDate > backDate) { // 选择的时间 大于 返程的时间  
							this.$store.commit('upHomeChoosedDateObj', {
								back: item.month + '月' + item.day + '日',
								back_full: item.full,
								back_obj: item
							});
						}
					} else {  // 没有返程时间  更新返程时间
						this.$store.commit('upHomeChoosedDateObj', {
							back: item.month + '月' + item.day + '日',
							back_full: item.full,
							back_obj: item
						});
					}

				}
				let key = this.goOrBack
				let key_full = this.goOrBack + '_full'
				let key_obj = this.goOrBack + '_obj'
				let chooesedTime = {
					[key]: item.month + '月' + item.day + '日',
					[key_full]: item.full,
					[key_obj]: item
				};
				this.$store.commit('upHomeChoosedDateObj', chooesedTime);
				this.curChoosedDayObj = item
				let date = calendar.getDateObj(this.curChoosedDayObj.full)
				uni.navigateBack({
					delta: 1
				});
			},
			init() {
				const curDay = calendar.getDateObj() // 当前日期
				const delayDay = Number(this.delayDay) || 0
				let delayDateObj = calendar.getBeforeDate(curDay.full, delayDay)
				// console.log('delayDateObj', delayDateObj);
				if (this.goOrBack !== 'go' && this.$store.getters.getHomeChoosedDateObj.go_obj && this.$store.getters.getHomeChoosedDateObj
					.go_obj.full) {
					delayDateObj = this.$store.getters.getHomeChoosedDateObj.go_obj.full
				}
				this.curChoosedDayObj = calendar.getDateObj(delayDateObj)
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
